package br.upe.dsc.caeto.core.cacheunit;

import br.upe.dsc.caeto.core.IRepository;
import java.util.Iterator;
import java.util.LinkedList;

public class CacheUnitRepositoryList  extends LinkedList<CacheUnit>
                                      implements IRepository<CacheUnit>,
                                      Iterable<CacheUnit> {
	
	public CacheUnitRepositoryList() {
		super();
	}
	
	public void insert(CacheUnit cache) {
		if (cache != null) {
			add(cache);
		}
	}
	
	public void remove(CacheUnit cache) {
		if (cache != null) {
			super.remove(cache);
		}
	}
	
	public void update(CacheUnit cache) {
		if (cache != null) {
			remove(cache);
			insert(cache);
		}
	}
	
	public CacheUnit search(String name) {
		if (name != null) {
			for (CacheUnit cache : this) {
				if (cache.getName().equals(name)) {
					return cache;
				}
			}
		}
		return null; //Throw an exception
	}
	
	public boolean isElement(String name) {
		if (name != null) {
			for (CacheUnit cache : this) {
				if (cache.getName().equals(name)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public void empty() {
		clear();
	}
	
	public Iterator<CacheUnit> iterator() {
		return listIterator();
	}
	
	public Iterable<CacheUnit> getElements() {
		return this;
	}
}
